package com.qut.graduate.busrtquery.utils.baidumap;

import android.os.Bundle;
import android.util.Log;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.Polyline;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.route.TransitRouteLine;
import com.qut.graduate.busrtquery.utils.common.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于显示换乘路线的Overlay，自3.4.0版本起可实例化多个添加在地图中显示
 */
public class TransitRouteOverlay extends OverlayManager {

    private BitmapDescriptor mBlueTexture = BitmapDescriptorFactory.fromAsset("icon_road_blue_middle.png");
    //    private BitmapDescriptor mWalkTexture = BitmapDescriptorFactory.fromAsset("icon_road_green_middle.png");
    private BitmapDescriptor mWalkTexture = BitmapDescriptorFactory.fromAsset("icon_road_walk.png");
    private List<BitmapDescriptor> textureList = new ArrayList<BitmapDescriptor>();
    private TransitRouteLine mRouteLine = null;

    /**
     * 构造函数
     *
     * @param baiduMap 该TransitRouteOverlay引用的 BaiduMap 对象
     */
    public TransitRouteOverlay(BaiduMap baiduMap) {
        super(baiduMap);
    }

    @Override
    public final List<OverlayOptions> getOverlayOptions() {
        if (mRouteLine == null) {
            return null;
        }
        List<OverlayOptions> overlayOptionses = new ArrayList<OverlayOptions>();
        // step node
        if (mRouteLine.getAllStep() != null && mRouteLine.getAllStep().size() > 0) {

            for (TransitRouteLine.TransitStep step : mRouteLine.getAllStep()) {

                Bundle b = new Bundle();
                b.putInt("index", mRouteLine.getAllStep().indexOf(step));
                if (step.getEntrance() != null) {
                    overlayOptionses.add((new MarkerOptions()).position(step.getEntrance().getLocation())
                            .anchor(0.5f, 0.5f).zIndex(10).extraInfo(b).icon(getIconForStep(step)));
                }
                // 最后路段绘制出口点
                if (mRouteLine.getAllStep().indexOf(step) == (mRouteLine.getAllStep().size() - 1) && step.getExit() != null) {
                    overlayOptionses.add((new MarkerOptions()).position(step.getExit().getLocation())
                            .anchor(0.5f, 0.5f).zIndex(10).icon(getIconForStep(step)));
                }
            }
        }
        if (mRouteLine.getStarting() != null) {
            overlayOptionses.add((new MarkerOptions()).position(mRouteLine.getStarting().getLocation())
                    .icon(getStartMarker() != null ? getStartMarker() :
                            BitmapDescriptorFactory.fromAssetWithDpi("icon_point_start.png")).zIndex(10));
        }
        if (mRouteLine.getTerminal() != null) {
            overlayOptionses.add((new MarkerOptions()).position(mRouteLine.getTerminal().getLocation())
                    .icon(getTerminalMarker() != null ? getTerminalMarker() :
                            BitmapDescriptorFactory.fromAssetWithDpi("icon_point_end.png")).zIndex(10));
        }

        // polyline
        if (mRouteLine.getAllStep() != null && mRouteLine.getAllStep().size() > 0) {
            List<Integer> textureIndexList = new ArrayList<Integer>();
            List<LatLng> points = new ArrayList<LatLng>();
            List<TransitRouteLine.TransitStep> steps = mRouteLine.getAllStep();
            int stepNum = steps.size();
            for (int i = 0; i < stepNum; i++) {
                if (i == stepNum - 1) {
                    points.addAll(steps.get(i).getWayPoints());
                } else {
                    points.addAll(steps.get(i).getWayPoints().subList(0, steps.get(i).getWayPoints().size() - 1));
                }
                if (steps.get(i).getStepType() == TransitRouteLine.TransitStep.TransitRouteStepType.WAKLING) {
                    OverlayOptions option = new PolylineOptions().width(20).points(steps.get(i).getWayPoints()).dottedLine(true).
                            customTexture(mWalkTexture);
                    overlayOptionses.add(option);
                } else if (steps.get(i).getStepType() == TransitRouteLine.TransitStep.TransitRouteStepType.BUSLINE
                        || steps.get(i).getStepType() == TransitRouteLine.TransitStep.TransitRouteStepType.SUBWAY) {
                    OverlayOptions option = new PolylineOptions().width(16).points(steps.get(i).getWayPoints()).dottedLine(true).
                            customTexture(mBlueTexture);
                    for (int j = 0; j < steps.get(i).getWayPoints().size(); j++) {
                        LogUtils.d("----TransitRouteOverlay steps.get(i).getWayPoints()：" + steps.get(i).getWayPoints().get(i));
                    }
                    overlayOptionses.add(option);
                }
            }
        }
        return overlayOptionses;
    }

    private BitmapDescriptor getIconForStep(TransitRouteLine.TransitStep step) {
        switch (step.getStepType()) {
            case BUSLINE:
                return BitmapDescriptorFactory.fromAssetWithDpi("icon_station_bus.png");
            case SUBWAY:
                return BitmapDescriptorFactory.fromAssetWithDpi("icon_station_subway.png");
            case WAKLING:
                return BitmapDescriptorFactory.fromAssetWithDpi("icon_station_walk.png");
            default:
                return null;
        }
    }

    /**
     * 设置路线数据
     *
     * @param routeOverlay 路线数据
     */
    public void setData(TransitRouteLine routeOverlay) {
        this.mRouteLine = routeOverlay;
    }

    /**
     * 覆写此方法以改变默认起点图标
     *
     * @return 起点图标
     */
    public BitmapDescriptor getStartMarker() {
        return null;
    }

    /**
     * 覆写此方法以改变默认终点图标
     *
     * @return 终点图标
     */
    public BitmapDescriptor getTerminalMarker() {
        return null;
    }

    public int getLineColor() {
        return 0;
    }

    /**
     * 覆写此方法以改变起默认点击行为
     *
     * @param i 被点击的step在{@link com.baidu.mapapi.search.route.TransitRouteLine#getAllStep()} 中的索引
     * @return 是否处理了该点击事件
     */
    public boolean onRouteNodeClick(int i) {
        if (mRouteLine.getAllStep() != null && mRouteLine.getAllStep().get(i) != null) {
            Log.i("baidumapsdk", "TransitRouteOverlay onRouteNodeClick");
        }
        return false;
    }

    @Override
    public final boolean onMarkerClick(Marker marker) {
        for (Overlay mMarker : mOverlayList) {
            if (mMarker instanceof Marker && mMarker.equals(marker)) {
                if (marker.getExtraInfo() != null) {
                    onRouteNodeClick(marker.getExtraInfo().getInt("index"));
                }
            }
        }
        return true;
    }

    @Override
    public boolean onPolylineClick(Polyline polyline) {
        // TODO Auto-generated method stub
        return false;
    }

}
